<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/12
 * Time: 11:51
 */
namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Constant\ErrorConstant;
use App\Service\Api\AddressService;
use Illuminate\Support\Facades\Validator;

class AddressController extends BaseController
{
    protected $service;

    public function __construct(Request $request, AddressService $service)
    {
        parent::__construct($request);
        $this->service  =   $service;
    }

    /**
     * 用户地址列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function listAddress()
    {
        [$flag, $result]    =   $this->service->addressList();
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_ADDRESS_LIST_ERROR, $result);
        }
    }

    /**
     * 用户添加地址
     * @return \Illuminate\Http\JsonResponse
     */
    public function appendAddress()
    {
        $validator  =   Validator::make($this->request->post(), [
            'userName'      =>  'required|string|max:50',
            'postalCode'    =>  'string|max:10',
            'provinceName'  =>  'required|string|max:50',
            'cityName'      =>  'required|string|max:50',
            'countyName'    =>  'required|string|max:50',
            'detailInfo'    =>  'required|string|max:100',
            'nationalCode'  =>  'string|max:30',
            'telNumber'     =>  'required|string|max:20',
        ], [
            'userName.required'  =>  '收货人姓名不能为空',
            'userName.string'    =>  '收货人姓名必须为字符',
            'userName.max'       =>  '收货人姓名过长',

            'postalCode.required'=>  '邮编不能为空',
            'postalCode.string'  =>  '邮编必须为字符',
            'postalCode.max'     =>  '邮编过长',

            'provinceName.required'  =>  '收货人省份不能为空',
            'provinceName.string'    =>  '收货人省份必须为字符',
            'provinceName.max'       =>  '收货人省份过长',

            'cityName.required'  =>  '收货人城市不能为空',
            'cityName.string'    =>  '收货人城市必须为字符',
            'cityName.max'       =>  '收货人城市过长',

            'countyName.required'=>  '收货人地区不能为空',
            'countyName.string'  =>  '收货人地区必须为字符',
            'countyName.max'     =>  '收货人地区过长',

            'detailInfo.required'=>  '收货人详细地址不能为空',
            'detailInfo.string'  =>  '收货人详细地址必须为字符',
            'detailInfo.max'     =>  '收货人详细地址过长',

            'nationalCode.required'  =>  '收货地址国家码不能为空',
            'nationalCode.string'    =>  '收货地址国家码必须为字符',
            'nationalCode.max'       =>  '收货地址国家码过长',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_ADDRESS_APPEND_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->addressAppend($this->request->post());
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_ADDRESS_APPEND_ERROR, $result);
        }
    }

    /**
     * 用户删除地址
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteAddress()
    {
        $validator  =   Validator::make($this->request->post(), [
            'address_id'      =>  'required|integer|exists:mini_user_address,id',
        ], [
            'address_id.required'  =>  '地址ID不能为空',
            'address_id.integer'   =>  '地址ID必须为整型',
            'address_id.exists'    =>  '地址不存在',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_ADDRESS_APPEND_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->addressDelete($this->request->post('address_id'));
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_ADDRESS_DELETE_ERROR, $result);
        }
    }

    /**
     * 用户编辑地址
     * @return \Illuminate\Http\JsonResponse
     */
    public function editAddress()
    {
        $validator  =   Validator::make($this->request->post(), [
            'address_id'    =>  'required|integer|exists:mini_user_address,id',
            'userName'      =>  'required|string|max:50',
            'postalCode'    =>  'string|max:10',
            'provinceName'  =>  'required|string|max:50',
            'cityName'      =>  'required|string|max:50',
            'countyName'    =>  'required|string|max:50',
            'detailInfo'    =>  'required|string|max:100',
            'nationalCode'  =>  'string|max:30',
            'telNumber'     =>  'required|string|max:20',
        ], [
            'address_id.required'=>  '地址ID不能为空',
            'address_id.integer' =>  '地址ID必须为整型',
            'address_id.exists'  =>  '地址不存在',

            'userName.required'  =>  '收货人姓名不能为空',
            'userName.string'    =>  '收货人姓名必须为字符',
            'userName.max'       =>  '收货人姓名过长',

            'postalCode.required'=>  '邮编不能为空',
            'postalCode.string'  =>  '邮编必须为字符',
            'postalCode.max'     =>  '邮编过长',

            'provinceName.required'  =>  '收货人省份不能为空',
            'provinceName.string'    =>  '收货人省份必须为字符',
            'provinceName.max'       =>  '收货人省份过长',

            'cityName.required'  =>  '收货人城市不能为空',
            'cityName.string'    =>  '收货人城市必须为字符',
            'cityName.max'       =>  '收货人城市过长',

            'countyName.required'=>  '收货人地区不能为空',
            'countyName.string'  =>  '收货人地区必须为字符',
            'countyName.max'     =>  '收货人地区过长',

            'detailInfo.required'=>  '收货人详细地址不能为空',
            'detailInfo.string'  =>  '收货人详细地址必须为字符',
            'detailInfo.max'     =>  '收货人详细地址过长',

            'nationalCode.required'  =>  '收货地址国家码不能为空',
            'nationalCode.string'    =>  '收货地址国家码必须为字符',
            'nationalCode.max'       =>  '收货地址国家码过长',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_ADDRESS_EDIT_ERROR, $validator->errors()->first());
        }

        $address_id     =   $this->request->post('address_id');
        $edit_param     =   $this->request->post();
        [$flag, $result]    =   $this->service->addressEdit((int) $address_id, $edit_param);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_ADDRESS_EDIT_ERROR, $result);
        }
    }

}
